/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.item;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.lineage.console.DataError;
import com.lineage.server.datatables.ItemActionTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.item.action.L1ItemAction;

/**
 * Class <code>ItemClass</code> 道具模组.
 * 
 * @author jrwz
 * @version 2012-6-19下午13:02:50
 * @see com.lineage.server.model.item
 * @since JDK1.7
 */
public final class ItemClass {

    /** 提示信息. */
    private static final Log LOG = LogFactory.getLog(ItemClass.class);

    /** ItemID执行类位置. */
    private static final Map<Integer, ItemExecutor> _classList = new HashMap<Integer, ItemExecutor>();

    /** 实例. */
    private static ItemClass instance;

    /**
     * 当实例为空时才新建一个.
     * 
     * @return 实例
     */
    public static ItemClass getInstance() {
        if (instance == null) {
            instance = new ItemClass();
        }
        return instance;
    }

    /**
     * 加入Class清单.
     * 
     * @param itemId
     *            道具编号
     * @param className
     *            执行类的位置
     */
    public void addList(final int itemId, final String className) {

        // Class为0
        if (className.equals("0")) {
            return;
        }

        try {
            final StringBuilder stringBuilder = new StringBuilder();
            stringBuilder
                    .append("com.lineage.server.model.item.etcitem.action.");
            stringBuilder.append(className);
            final Class<?> cls = Class.forName(stringBuilder.toString());
            final ItemExecutor exe = (ItemExecutor) cls.getMethod("get")
                    .invoke(null);
            // XXX 测试 Integer.valueOf
            _classList.put(Integer.valueOf(itemId), exe);
        } catch (final ClassNotFoundException e) {
            final String error = "道具Class档案 (" + className + ") 错误 (ItemId:"
                    + itemId + ")";
            LOG.error(error);
            DataError.isError(LOG, error, e);
        } catch (final IllegalArgumentException e) {
            LOG.error(e.getLocalizedMessage(), e);
        } catch (final IllegalAccessException e) {
            LOG.error(e.getLocalizedMessage(), e);
        } catch (final InvocationTargetException e) {
            LOG.error(e.getLocalizedMessage(), e);
        } catch (final SecurityException e) {
            LOG.error(e.getLocalizedMessage(), e);
        } catch (final NoSuchMethodException e) {
            LOG.error(e.getLocalizedMessage(), e);
        }
    }

    /**
     * 道具动作执行.
     * 
     * @param pc
     *            - 对象
     * @param useItem
     *            - 使用的物件
     * @param readC1
     *            - readC1
     * @param readC2
     *            - readC2
     * @param targetId
     *            - 选取目标的OBJID
     * @param targetX
     *            - X坐标
     * @param targetY
     *            - Y坐标
     */
    public void item(final L1PcInstance pc, final L1ItemInstance useItem,
            final int readC1, final int readC2, final int targetId,
            final int targetX, final int targetY) {

        try {
            // 取得Class执行位置
            // XXX 测试 Integer.valueOf(item.getItemId());
            final ItemExecutor exe = _classList.get(Integer.valueOf(useItem
                    .getItemId()));
            if (exe != null) {
                exe.execute(pc, useItem, readC1, readC2, targetId, targetX,
                        targetY);
            }
        } catch (final Exception e) {
            LOG.error(e.getLocalizedMessage(), e);
        }
    }

    /**
     * 道具动作执行~XML.
     * 
     * @param pc
     *            - 对象
     * @param useItem
     *            - 使用的物件
     * @param readC1
     *            - readC1
     * @param readC2
     *            - readC2
     * @param targetId
     *            - 选取目标的OBJID
     * @param targetX
     *            - X坐标
     * @param targetY
     *            - Y坐标
     */
    public void itemXML(final L1PcInstance pc, final L1ItemInstance useItem,
            final int readC1, final int readC2, final int targetId,
            final int targetX, final int targetY) {
        try {
            final L1ItemAction action = ItemActionTable.getInstance().get(pc,
                    useItem);
            if (action != null) {
                action.execute(pc, useItem, readC1, readC2, targetId, targetX,
                        targetY);
            }
        } catch (final Exception e) {
            LOG.error(e.getLocalizedMessage(), e);
        }
    }
}
